import { createStore } from 'vuex';
import axios from 'axios';

const store = createStore({
  state() {
    return {
      routes: [],
      isLogin: false,
    };
  },
  mutations: {
    SET_ROUTES(state, route) {
      state.route = route;
    },
    SET_LOGIN(state, bool) {
      state.isLogin = bool;
    },
  },
  actions: {
    getRoutes({ commit }) {
      return new Promise((resolve) => {
        axios.get('../routes.json').then((res) => {
          const data = res.data.data;
          const routes = data.map((item) => {
            return {
              path: item.path,
              name: item.name,
              component: () => import(`../views/${item.name}.vue`),
            };
          });
          commit('SET_ROUTES', routes);
          resolve(routes);
        });
      });
    },
    login({ commit }) {
      return new Promise((reslove) => {
        commit('SET_LOGIN', true);
        reslove();
      });
    },
    logOut({ commit }) {
      return new Promise((reslove) => {
        commit('SET_LOGIN', false);
        reslove();
      });
    },
  },
});

export default store;
